package com.yuyou.tas.terminal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.stream.Collectors;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.service.api.lesson.req.ReqFilterOrganStuOIds;
import com.yuyou.tas.service.api.lesson.service.StudentAPI;
import com.yuyou.tas.terminal.comm.CustomerBindStudentVo;
import com.yuyou.tas.terminal.comm.MUAPI;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.exception.BusinessException;

@Api(value = "YyfnMUController", description = "学生端公共接口")
@Validated
@RestController
@RequestMapping(value = "/api/m/u")
public class YyfnMUController extends BaseController {

	@Autowired
	private StudentAPI studentAPI;

	@Autowired
	private MUAPI mUAPI;

	protected Student getTasStudent(String accessToken, String sysCode,
			Long studentId) throws Exception {

		// TODO remove
		if (accessToken.equals("feedback_pc")) {
			Student student = studentAPI.selectByPrimaryKey(studentId);
			if (student == null) {
				throw new BusinessException("没找到该学生信息");
			}
			return student;
		} else {
			// 转换学生ID
			Long authOrganizationId = this.getOrganId(sysCode);
			Student student = studentAPI.selectByOrgan7OId(studentId,
					authOrganizationId);
			if (student == null) {
				throw new BusinessException("没找到该学生信息");
			}
			return student;
		}
	}

	@ApiOperation(value = "查询绑定学生列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "accessToken", value = "accessToken", paramType = "query", required = true),
			@ApiImplicitParam(name = "sysCode", value = "机构编号", paramType = "query", required = true) })
	@RequestMapping(value = "/findCustomerBindStudent", method = {
			RequestMethod.GET, RequestMethod.POST })
	public ReturnResult<Student> findCustomerBindStudent(
			@NotBlank(message = "accessToken不能为空") String accessToken,
			@NotBlank(message = "sysCode不能为空") String sysCode) throws Exception {

		CustomerBindStudentVo vo = mUAPI.findCustomerBindStudent(accessToken);
		if (!vo.isSuccess() && vo.getDesc().indexOf("accessToken") != -1) {
			return ReturnResult.fail3001();
		}
		if (!vo.isSuccess()) {
			return ReturnResult.returnRes(vo.isSuccess(), vo.getDesc(), null);
		}
		// 过滤机构
		List<Long> oIds = vo.getData().stream()
				.map(item -> item.getStudentId()).collect(Collectors.toList());

		Long authOrganizationId = this.getOrganId(sysCode);
		List<Student> s = studentAPI
				.filterOrganByOIds(new ReqFilterOrganStuOIds(oIds,
						authOrganizationId));
		return ReturnResult.success(s);
	}

}
